﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Xml;

namespace  MyFloridaHouse.libPrint.KML
{
    public static class KMLSerialFunctions
    {


        /// <summary>
        /// Deserialization methodo for kml.
        /// </summary>
        /// <param name="myString">string</param>
        /// <returns></returns>
        public static kml DeserializeKML(string myFileString)
        {
            try
            {
                XmlSerializer mySerializer = new XmlSerializer(typeof(kml));

                byte[] byteArray = Encoding.ASCII.GetBytes(myFileString);
                MemoryStream myFileStream = new MemoryStream(byteArray);


                //WHOLE REPLACEMENT BIT
                StreamReader sr = new StreamReader(myFileStream);
                MemoryStream outStream = new MemoryStream();
                StreamWriter sw = new StreamWriter(outStream);
                String outString;
                StringBuilder sb = new StringBuilder();
                int i = 1;
                while (!sr.EndOfStream)
                {
                    outString = sr.ReadLine().Replace(@"\", String.Empty);
                    if (outString.IndexOf("kml xmlns")>0)
                    {
                        int index = outString.IndexOf("<Document>");
                        string subString = outString.Substring(index, outString.Length - index);
                        outString = @"<kml xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">" + subString;
                    }
                    i++;
                    sb.Append(outString);
                }
                string finalShapeString;
                finalShapeString = sb.ToString().Replace(@"\", String.Empty);
                byte[] newByteArray = Encoding.ASCII.GetBytes(finalShapeString);
                MemoryStream stream = new MemoryStream(newByteArray);
                myFileStream = stream;
                return (kml)mySerializer.Deserialize(myFileStream);

            }
            catch (Exception ex)
            {
                string error = ex.ToString();
                return new kml();
            }
        }

        static string ConvertStringArrayToString(string[] array)
        {
            //
            // Concatenate all the elements into a StringBuilder.
            //
            StringBuilder builder = new StringBuilder();
            foreach (string value in array)
            {
                builder.Append(value);
            }
            return builder.ToString();
        }

        
        
        
        /// <summary>
        /// Deserialization methodo for kml.
        /// </summary>
        /// <param name="myString">stream</param>
        /// <returns></returns>
        public static kml DeserializeKML(Stream myFileStream)
        {
            try
            {
                XmlSerializer mySerializer = new XmlSerializer(typeof(kml));

                //replace w/ your relevent KML stream
                //Stream myFileStream = FileUpload1.FileContent;

                //WHOLE REPLACEMENT BIT
                StreamReader sr = new StreamReader(myFileStream);
                MemoryStream outStream = new MemoryStream();
                StreamWriter sw = new StreamWriter(outStream);
                String outString;
                StringBuilder sb = new StringBuilder();
                int i = 1;
                while (!sr.EndOfStream)
                {
                    outString = sr.ReadLine().Replace(@"\", String.Empty);
                    if (i == 2)
                    {
                        outString = @"<kml xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">";
                    }
                    i++;
                    if (!outString.Contains("atom:"))  //deserializer can't handle atom namespace, we don't need any info on it
                    {
                        sb.Append(outString);
                    }
                }
                byte[] byteArray = Encoding.ASCII.GetBytes(sb.ToString().Replace(@"\", String.Empty));
                MemoryStream stream = new MemoryStream(byteArray);
                myFileStream = stream;
                return (kml)mySerializer.Deserialize(myFileStream);
            }
            catch (Exception ex)
            {
                string error = ex.ToString();
                return new kml();
            }
        }


    }
}